package com.tanhua.dubbo.api;

import com.tanhua.model.enums.CommentType;
import com.tanhua.model.mongo.Comment;
import com.tanhua.model.mongo.Movement;
import org.apache.dubbo.config.annotation.DubboService;
import org.bson.types.ObjectId;
import org.checkerframework.checker.units.qual.C;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.FindAndModifyOptions;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;

import java.util.List;

@DubboService
public class CommentsApiImpl implements CommentsApi {

    @Autowired
    private MongoTemplate mongoTemplate;

    /*圈子-发布评论/点赞/喜欢*/
    @Override
    public Integer save(Comment comment) {
        //通过publishId查询动态
        Movement movement = mongoTemplate.findById(comment.getPublishId(), Movement.class);
        //获取被评论人id并更新comment
        comment.setPublishUserId(movement.getUserId());
        //新增评论/点赞/喜欢
        mongoTemplate.save(comment);
        //更新动态表的对应字段信息
        Query query = Query.query(Criteria.where("id").is(comment.getPublishId()));
        Update update = new Update();
        if (comment.getCommentType() == CommentType.LIKE.getType()) {
            update.inc("likeCount", 1);
        } else if (comment.getCommentType() == CommentType.COMMENT.getType()) {
            update.inc("commentCount", 1);
        } else {
            update.inc("loveCount", 1);
        }
        //设置更新参数
        FindAndModifyOptions options = new FindAndModifyOptions();
        options.returnNew(true);
        //获取更新后的最新数据
        Movement modify = mongoTemplate.findAndModify(query, update, options, Movement.class);
        //获取最新的点赞/评论/喜欢的总数，并返回
        return modify.statisCount(comment.getCommentType());
    }

    /*圈子-评论列表*/
    @Override
    public List<Comment> findComments(String movementId, CommentType commentType, Integer page, Integer pagesize) {
        //构造查询条件
        Query query = Query.query(Criteria.where("publishId").is(new ObjectId(movementId))
                        .and("commentType").is(commentType.getType()))
                .skip((page - 1) * pagesize)
                .limit(pagesize)
                .with(Sort.by(Sort.Order.desc("created")));
        //查询并返回结果
        List<Comment> comments = mongoTemplate.find(query, Comment.class);
        return comments;
    }

    /*判断用户是否已点赞/喜欢*/
    @Override
    public Boolean hasComment(Long userId, String movementId, CommentType commentType) {
        //构造查询条件
        Criteria criteria = Criteria.where("userId").is(userId)
                .and("publishId").is(new ObjectId(movementId))
                .and("commentType").is(commentType.getType());
        Query query = Query.query(criteria);
        //查询并返回结果
        return mongoTemplate.exists(query, Comment.class);
    }

    /*圈子-取消点赞/喜欢*/
    @Override
    public Integer delete(Comment comment) {
        //删除点赞/喜欢数据
        Criteria criteria = Criteria.where("userId").is(comment.getUserId())
                .and("publishId").is(comment.getPublishId())
                .and("commentType").is(comment.getCommentType());
        Query query1 = Query.query(criteria);
        mongoTemplate.remove(query1, Comment.class);
        //更新动态表的对应字段信息
        Query query2 = Query.query(Criteria.where("id").is(comment.getPublishId()));
        Update update = new Update();
        if (comment.getCommentType() == CommentType.LIKE.getType()) {
            update.inc("likeCount", -1);
        } else if (comment.getCommentType() == CommentType.COMMENT.getType()) {
            update.inc("commentCount", -1);
        } else {
            update.inc("loveCount", -1);
        }
        //设置更新参数
        FindAndModifyOptions options = new FindAndModifyOptions();
        options.returnNew(true);
        //获取更新后的最新数据
        Movement modify = mongoTemplate.findAndModify(query2, update, options, Movement.class);
        //获取最新的点赞/评论/喜欢的总数，并返回
        return modify.statisCount(comment.getCommentType());
    }


    /*查询点赞信息*/
    @Override
    public List<Comment> findById(Long userId, Integer page, Integer pagesize,int commentType) {
        //构造查询条件
        Criteria criteria = Criteria.where("publishUserId").is(userId).and("commentType").is(commentType);
        //构造分页和排序
        Query query = Query.query(criteria)
                .skip((page-1)*pagesize).limit(pagesize)
                .with(Sort.by(Sort.Order.desc("created")));
        //查询并返回结果
        List<Comment> list = mongoTemplate.find(query, Comment.class);
        return list;
    }

    /*查询喜欢信息*/

}
